Assignment: a2_Lecture Examples By: Daniel B. Carr Examples: 1. Emerging circles 2. Cognitively constructed contours revisited 3. Difference between curves 4. Position along a scale Due: Figures from 1, 2, 3.1, 3.2, 4 Revised script from 3.2 with changes in red 1. Emerging circles Purpose Illustrate rivalry for visual attention of results at different scales from parallel human visual processing References Jose Luis Marroquin 1976. "Human Visual Perception of Structure." MIT MS Thesis David Marr 1982 "Vision" W. H Freeman and Company. New York Steps Generate a square lattice Remove lattice points outside a circle Plot Rotate clipped lattice 60 degrees and overplot Rotate clipped lattice 120 degrees and overplot ##Run # generate square lattice, note expand.grid() below size = 15 x = -size:size # The integer sequence from -15 to 15 y = seq(-15,15,by=1) # Another way to do the same thing coordList = list(x,y) # Include more vectors for higher dimensions lattice = expand.grid(coordList) # produces a data.frame lattice = as.matrix(lattice) # convert the data.frame to a matrix distance = sqrt( (lattice^2) %*% rep(1,ncol(lattice)) ) # Here each row in lattice represents a 2D point # lattice^2 squares all elememt (coordinates) in the matrix # %*% is matrix multiply # rep(1,ncol(lattice)) is a vector of 1's # the matrix product is the sum of squares for each row # sqrt() the gives each point's distance from the origin good = distance <= size # good is TRUE when distance is less than or equal to size # size, a single value, is replicated as necessary clippedLattice = lattice[good,] # device and plot setup windows() par(pty='s') # ensures a square plot plot(c(-size,size),c(-size,size),type='n',axes=F, xlab='',ylab='',main='Emerging Circles') #______________Rotating points in a data table____________ # # A data table has cases as rows and variables as columns # This is common in statistical packages # # Matrix-oriented computer graphics transformations # are often expressed (after conversion to R syntax) as # Transform %*% t(DataTable) # %*% means matrix multiplication in R # t() mean transpose in R # The results have variables as rows # # We will need to transpose results back to data table orientation # for our R plotting commands # # Note that since t(A%*%B)= t(B) %*% t(A) # the desired results # t(Transform%*%t(DataTable)) # can be obtains as DataTable %*% t(Transform) # # In other words we can # construct the usual computer graphics transform: Transform # transpose it t(Transform) # postmultiply DataTable %*% t(Transform) # and plot the columns # Create a function for producting rotation matrices rotMat = function(angle,degrees=F){ if(degrees)angle=pi*angle/180 mat = matrix(c( cos(angle), sin(angle), -sin(angle), cos(angle)), byrow=T,ncol=2) return(mat) } RotMat60 = rotMat(60,deg=T) RotMat120 = rotMat(120,deg=T) # compute and plot cex = .5 pch = 16 points(clippedLattice,pch=pch,cex=cex) points(clippedLattice %*% t(RotMat60),pch=pch,cex=cex) points(clippedLattice %*% t(RotMat120),pch=pch,cex=cex) ##End 2. Cognitively constructed contours Reference: David Marr 1982. "Vision" W. H Freeman and Company. New York # The following is obscure. # Don't worry about understanding this one # I was in a hurry and didn't have # Bezier curves at hand so tried using Lagrange # polynomials to construct curves via # trial and error placement of points. trial and error ## Run rx = c(-10,10) curveFit= function(v,x,y){ return( y[3]*((v-x[1])*(v-x[2]))/((x[3]-x[1])*(x[3]-x[2]))+ y[2]*((v-x[1])*(v-x[3]))/((x[2]-x[1])*(x[2]-x[3]))+ y[1]*((v-x[2])*(v-x[3]))/((x[1]-x[2])*(x[1]-x[3]))) } # triangle 1 x1 = c(-8,-1,-5.5) y1 = c(-4,-4,7) #triangle 2 x3 = c(1,4.5,9.5) y3 = c(-4,9,-4) v2 = seq(.5,5.5,length=100) v3 = seq(5.5,9,length=100) crv2 = curveFit(v2,c(.5,4.5,5.5),c(6,-2.5,-6.5)) crv3 = curveFit(v3,c(5.5,8,9),c(-6.5,4.0,6)) #lines(v3,crv3) #lines(v2,crv2) # black circles ang = seq(0,2*pi,length=100) r = 1.2 x2 = r*cos(ang) y2 = r*sin(ang) v = seq(-8.5,-0.1,length=100) x = c(-8.5,-5,-0.1) y = c(.5,1.2,1) crv1 = curveFit(v,x,y) windows() par(pty='s') plot(rx,rx,type='n',axes=F,xlab='Perceived But Absent Edges', ylab='',cex=2) polygon(x1,y1,density=0,col="black") polygon(x2-8.5,y2+.5,col="black") polygon(x2-.1,y2+1,col="black") polygon(x2-5,y2-5.5,col="black") px1 =c(v,-5,-8.5) py1 = c(crv1,-5.5,.5) polygon(px1,py1,border=NA,col="white") polygon(x3,y3,density=0,col="black") polygon(x2+.5,y2+6,col="black") polygon(x2+5.5,y2-6.5,col="black") polygon(x2+9,y2+6,col="black") px2 = c(v2,v3,.5) py2 = c(crv2,crv3,6) polygon(px2,py2,border=NA,col="white") ##End 3. Difference between two curves Reference: William S. Cleveland, 1994. "The Elements of Graphing Data" Hobart Press, Summit, New Jersey. # ______________________Data___________________________ # This was captured earlier using a construct sketch() function x = seq(0,1,length=21) y2 = c(.04,.041,.045,.045,.055,.075,.13,.27, .44,.61,.78,.91,.95,.97,.972,.974, .977,.978,.98,.985,.985) y1 = c(0.005,0.005,0.008,0.011,0.016,0.022,0.063, 0.185,0.342,0.502,0.654,0.840,0.898,0.918, 0.916,0.930,0.927,0.936,.939,.945,.956) curveDif = y2-y1 3.1 Version 1 without tick marks ## Run # Graph device and parameters windows() cex = .9 # panel layout panels = panelLayout(nrow=2,ncol=1,rowSep=c(0,.2,0),leftMar=.42) # plot two curves and difference rx = c(-.04,1.04) ry = c(-.04,1.04) xgrid = seq(0,1,by=.2) ygrid = c(0,.25,.5,.75,1) panelSelect(panels,1,1) panelScale(rx,ry) panelFill() panelGrid(x=xgrid,y=ygrid,col="white",lwd=2) axis(side=2,at=ygrid,tck=0,mgp=c(2,.3,0),las=2,cex=cex) # mtext() could be used instead # mtext(side=2,line=.3,text=format(ygrid),at=ygrid,las=2,cex=cex) lines(x,y1,lwd=3) lines(x,y2,lwd=3) lines(x,curveDif,lwd=3,col="blue") panelOutline() mtext(side=3,line=2.4,'Top Panel: Two Curves and Their Difference',cex=1.3) mtext(side=3,line=.7,'Bottom Panel: Zoomed View of the Difference',cex=1.3) # Rescale and plot difference ry = c(.02,.14) ygrid=c(.02,.05,.08,.11,.14) panelSelect(panels,2,1) panelScale(rx,ry) panelFill() panelGrid(x=xgrid,y=ygrid,col="white",lwd=2) axis(side=1,at=xgrid,mgp=c(2,.2,0),tck=0,las=1,cex=cex) axis(side=2,at=ygrid,mgp=c(2,.3,0),tck=0,las=2,cex=cex) lines(x,curveDif,lwd=3,col="blue") panelOutline() ##End 3.2 Version 2 Add ticks and adjust tick labeling________________ Insert variables in axis()arguments below to control 1) the direction and size of the ticks 2) the separation of the tick label from the axis The arguments to change are tck and mgp For the bottom x-axis (side = 1) the variable names might be xAxisTck xAxisTckLabSep Then the arguments would look something like tck=xAxisTck mgp=c(2,xAxisTckLabSep,0) Then define the variables in the graphics parameter section of the script. The lines to change have a ? in them. You don't have to use the two names I suggested, just make it work with the axes looking reasonably nice. The tcks should point outward to follow Cleveland's guidance. You might start experimenting with something like xAxisTck=-.03. The negative makes the tck point outward. For the x-axis (side 1 or 3) the value is a fraction the multiplies the panel height. For the y-axis (side 2 or 4) the value is a fraction that multiplies the panel width. ## Run # Graph Device and parameters windows() cex = .9 xAxisTck = ? xAxisTckLabSep = ? ?name = ?value # These are for the y-axis ?name = ?value # panel layout panels = panelLayout(nrow=2,ncol=1,rowSep=c(0,.2,0),leftMar=.42) # plot two curves and difference rx = c(-.04,1.04) ry = c(-.04,1.04) xgrid = seq(0,1,by=.2) ygrid = c(0,.25,.5,.75,1) panelSelect(panels,1,1) panelScale(rx,ry) panelFill() panelGrid(x=xgrid,y=ygrid,col="white",lwd=2) axis(side=2,at=ygrid,tck=0,mgp=c(2,.3,0),las=2,cex=cex) # mtext() could be used instead # mtext(side=2,line=.3,text=format(ygrid),at=ygrid,las=2,cex=cex) lines(x,y1,lwd=3) lines(x,y2,lwd=3) lines(x,curveDif,lwd=3,col="blue") panelOutline() mtext(side=3,line=2.4,'Top Panel: Two Curves and Their Difference',cex=1.3) mtext(side=3,line=.7,'Bottom Panel: Zoomed View of the Difference',cex=1.3) # Rescale and plot difference ry = c(.02,.14) ygrid=c(.02,.05,.08,.11,.14) panelSelect(panels,2,1) panelScale(rx,ry) panelFill() panelGrid(x=xgrid,y=ygrid,col="white",lwd=2) axis(side=1,at=xgrid,mgp=c(2,.2,0),tck=0,las=1,cex=cex) axis(side=2,at=ygrid,mgp=c(2,.3,0),tck=0,las=2,cex=cex) lines(x,curveDif,lwd=3,col="blue") panelOutline() ##End 4. Position along a scale, proximity and grid lines Reference: W.S. Cleveland, and R. McGill. 1984. Graphical Perception: Theory, Experimentation, and Application to the Development of Graphical Methods. Journal of the American Statistical Association. ##Run windows(width=10,height=6.5) pan = panelLayout(nrow=1,ncol=3, topMar=1.,bottomMar=0.9, colSep=c(0,.3,.3,0)) # Graph Parameters cexP = 1.7 # size: point cexT= 2.0 # size: title cexA = 1.5 # size: axis tick lables and text line1 = 2.3 # x axis label placement line2 = 3.8 # x axis label placement # plot title panelSelect(pan,mar='top') panelScale() text(.5,.84,'Perceptual Accuracy Of Extraction:',adj=.5,cex=cexT,col="blue") text(.5,.45,'Position Along A Scale Is Best',adj=.5,cex=cexT, col="blue") # plot first of three columns______________________________ # points close to axis rx = c(0,4) panelSelect(pan,1,1) panelScale(rx,rx) points(c(1,2.5),c(.2,.2),pch=16,cex=cexP) axis(side=1,at=0:4,mgp=c(2,.6,0),tck=-.02,cex.axis=cexA ) mtext(side=1,line=line1,'Accurate decoding',cex=cexA) # points away from axis panelSelect(pan,1,2) panelScale(rx,rx) points(c(1,2.5),c(2,3.5),pch=16,col="black",cex=cexP) axis(side=1,at=0:4,cex.axis=cexA,mgp=c(2,.6,0),tck = -.02,col="black") panelOutline() mtext(side=1,line=line1,'Increased distance',col="black",cex=cexA) mtext(side=1,line=line2,'Decreased accuracy',col="black",cex=cexA) # points away from axis plus grid lines panelSelect(pan,1,3) panelScale(rx,rx) panelFill(col="#BFBFBF") abline(v=0:4,col="white",lwd=2) points(c(1,2.5),c(2,3.5),pch=16,col="black",cex=cexP) axis(side=1,at=0:4,mgp=c(2,.6,0),tck=-.02,cex.axis=cexA) panelOutline() mtext(side=1,line=line1,'Grid lines help',cex=cexA) ##End